package mark.tom.pistols.decryptor;

import java.util.ArrayList;
import java.util.List;

import mark.tom.pistols.algorithm.encryption.Base64Encryption;
import mark.tom.pistols.model.SecurityModelProto;

public final class KeyChainClearer {

    public static KeyChainModel getDecryptKey(String modelContent) throws Exception {
        if (null == modelContent || modelContent.trim().length() == 0) {
            return null;
        }
        SecurityModelProto.SecurityModel securityModel = SecurityModelProto.SecurityModel.parseFrom(Base64Encryption.decrypt(modelContent));
        // 拼装数据模型
        KeyChainModel keyChainModel = new KeyChainModel();
        keyChainModel.setDecryptKey(new StringBuilder(securityModel.getDecryptKey()).reverse().toString());
        List<SecurityModelProto.SecurityModel.EncryptFile> encryptFiles = securityModel.getEncryptFiles();
        if (null != encryptFiles && encryptFiles.size() > 0) {
            List<String> fileNames = new ArrayList<String>();
            for (SecurityModelProto.SecurityModel.EncryptFile encryptFile : encryptFiles) {
                fileNames.add(encryptFile.getName());
            }
            keyChainModel.setFileNames(fileNames);
        }
        return keyChainModel;
    }
}
